#ifndef URL_INC
#define URL_INC

#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

/**
 * @brief urlfmt, 根据cwd 与 url 生成一个绝对路径，以 / 结尾
 * @param cwd 当前工作目录
 * @param url 待处理的url
 * @param out 输出
 */
static void urlfmt(const char* cwd,
                   const char* url,
                   char* out,
                   size_t out_size);

static void urlfmt(const char* cwd,
                   const char* url,
                   char* out,
                   size_t out_size) {
#ifdef _WIN32
#define isAbsolute(x) (isalpha(x[0]) && x[1] == ':')
#else
#define isAbsolute(x) (x[0] == '/')
#endif

  memset(out, 0, out_size);
  if (isAbsolute(url)) {
    size_t urllen = strlen(url);
    memcpy(out, url, urllen);
    if (url[urllen - 1] != '/')
      out[urllen] = '/';
    return;
  }
  size_t urllen = strlen(url);
  size_t cwdlen = strlen(cwd);
  memcpy(out, cwd, cwdlen);
  if (cwd[cwdlen - 1] != '/') {
    out[cwdlen] = '/';
    cwdlen++;
  }
  memcpy(out + cwdlen, url, urllen);
  if (url[urllen - 1] != '/')
    out[cwdlen + urllen] = '/';
  return;
}

#endif